using System;
using NUnit.Framework;
using WatiN.Core;
using Wax;

namespace WaxLibTest
{
    /// <summary>
    /// Test the behaviour of the <see cref="SettingsCommand"/> class.
    /// </summary>
    [TestFixture]
    public class SettingsCommandTest
    {
        /// <summary>
        /// Test that the command factory correctly returns a SettingsCommand given the correct command value
        /// </summary>
        [Test]
        public void ParseSettingsCommand()
        {
            Command settingsCommand = CommandFactory.Parse(new string[] { "UpdateSettings", "Settings", "HighlightColor,Red", string.Empty, string.Empty });
            Assert.AreEqual(typeof(SettingsCommand).FullName, settingsCommand.GetType().FullName, "CommandFactory.Parse did not return a settings command.");
        }

        /// <summary>
        /// Test that we can retrieve the settings name and value from the parsed object
        /// </summary>
        [Test]
        public void SettingsNameAndValue()
        {
            SettingsCommand settingsCommand = (SettingsCommand) CommandFactory.Parse(new string[] { "UpdateSettings", "Settings", "HighlightColor,Red", string.Empty, string.Empty });

            Assert.AreEqual(SettingsType.HighlightColor, settingsCommand.SettingsType);
            Assert.AreEqual("Red", settingsCommand.Value);
        }

        /// <summary>
        /// Test that we can change the attach to IE time out setting.
        /// </summary>
        [Test]
        public void ChangeAttachToIETimeOutSetting()
        {
            using (TestManager testManager = new TestManager())
            {
                testManager.Tests.Add(new Test());
                testManager.Tests[0].Commands.Add(CommandFactory.Parse(new string[] { "UpdateSettings", "Settings", "AttachToIETimeout,60", string.Empty, string.Empty }));
                testManager.Execute();
              
                Assert.AreEqual(60, IE.Settings.AttachToIETimeOut);
            }
        }

        /// <summary>
        /// Test that we can change the AutoCloseDialogs setting.
        /// </summary>
        [Test]
        public void ChangeAutoCloseDialogsSetting()
        {
            using (TestManager testManager = new TestManager())
            {
                testManager.Tests.Add(new Test());
                testManager.Tests[0].Commands.Add(CommandFactory.Parse(new string[] { "UpdateSettings", "Settings", "AutoCloseDialogs,false", string.Empty, string.Empty }));
                testManager.Execute();

                Assert.AreEqual(false, IE.Settings.AutoCloseDialogs);
            }
        }

        /// <summary>
        /// Test that we can change the AutoMoveMousePointerToTopLeft setting.
        /// </summary>
        [Test]
        public void ChangeAutoMoveMousePointerToTopLeftSetting()
        {
            using (TestManager testManager = new TestManager())
            {
                testManager.Tests.Add(new Test());
                testManager.Tests[0].Commands.Add(CommandFactory.Parse(new string[] { "UpdateSettings", "Settings", "AutoMoveMousePointerToTopLeft,false", string.Empty, string.Empty }));
                testManager.Execute();

                Assert.AreEqual(false, IE.Settings.AutoMoveMousePointerToTopLeft);
            }
        }

        /// <summary>
        /// Test that we can change the AutoStartDialogWatcher setting.
        /// </summary>
        [Test]
        public void ChangeAutoStartDialogWatcherSetting()
        {
            using (TestManager testManager = new TestManager())
            {
                testManager.Tests.Add(new Test());
                testManager.Tests[0].Commands.Add(CommandFactory.Parse(new string[] { "UpdateSettings", "Settings", "AutoStartDialogWatcher,false", string.Empty, string.Empty }));
                testManager.Execute();

                Assert.AreEqual(false, IE.Settings.AutoStartDialogWatcher);
            }
        }

        /// <summary>
        /// Test that we can change the HighlightColor setting.
        /// </summary>
        [Test]
        public void ChangeHighLightColorSetting()
        {
            using (TestManager testManager = new TestManager())
            {
                testManager.Tests.Add(new Test());
                testManager.Tests[0].Commands.Add(CommandFactory.Parse(new string[] { "UpdateSettings", "Settings", "HighlightColor,Red", string.Empty, string.Empty }));
                testManager.Tests[0].Commands.Add(CommandTest.CreateCommandGotoUrl(System.IO.Path.Combine(System.Environment.CurrentDirectory, "testpage1.htm")));
                testManager.Tests[0].Commands.Add(CommandFactory.Parse(new String[] { "FlashTest", "Flash", string.Empty, "ByNameAndValue", "radio1,2" }));                
                testManager.Execute();

                Assert.AreEqual("Red", IE.Settings.HighLightColor);
            }
        }

        /// <summary>
        /// Test that we can change the HighlightElement setting.
        /// </summary>
        [Test]
        public void ChangeHighLightElementSetting()
        {
            using (TestManager testManager = new TestManager())
            {
                testManager.Tests.Add(new Test());
                testManager.Tests[0].Commands.Add(CommandFactory.Parse(new string[] { "UpdateSettings", "Settings", "HighlightElement,false", string.Empty, string.Empty }));
                testManager.Execute();

                Assert.AreEqual(false, IE.Settings.HighLightElement);
            }
        }

        /// <summary>
        /// Test that we can change the MakeNewIEInstanceVisible setting.
        /// </summary>
        [Test]
        public void ChangeMakeNewIeInstanceVisibleSetting()
        {
            using (TestManager testManager = new TestManager())
            {
                testManager.Tests.Add(new Test());
                testManager.Tests[0].Commands.Add(CommandFactory.Parse(new string[] { "UpdateSettings", "Settings", "MakeNewIEInstanceVisible,false", string.Empty, string.Empty }));
                testManager.Execute();

                Assert.AreEqual(false, IE.Settings.MakeNewIeInstanceVisible);
            }
        }

        /// <summary>
        /// Test that we can change the WaitForCompleteTimeout setting.
        /// </summary>
        [Test]
        public void ChangeWaitForCompleteTimeOutSetting()
        {
            using (TestManager testManager = new TestManager())
            {
                testManager.Tests.Add(new Test());
                testManager.Tests[0].Commands.Add(CommandFactory.Parse(new string[] { "UpdateSettings", "Settings", "WaitForCompleteTimeout,60", string.Empty, string.Empty }));
                testManager.Execute();

                Assert.AreEqual(60, IE.Settings.WaitForCompleteTimeOut);
            }
        }

        /// <summary>
        /// Test that we can change the WaitUntilExistsTimeout setting.
        /// </summary>
        [Test]
        public void ChangeWaitUntilExistsTimeOutSetting()
        {
            using (TestManager testManager = new TestManager())
            {
                testManager.Tests.Add(new Test());
                testManager.Tests[0].Commands.Add(CommandFactory.Parse(new string[] { "UpdateSettings", "Settings", "WaitUntilExistsTimeout,60", string.Empty, string.Empty }));
                testManager.Execute();

                Assert.AreEqual(60, IE.Settings.WaitUntilExistsTimeOut);
            }
        }
    }
}